<?php
namespace Backend\Controllers;

use Models\Wx;
use Models\Coupon;
use Models\CouponCard;
use Models\CouponUser;

class AppcouponController extends BaseController
{
    public function indexAction()
    {
        $currentPage = $this->request->getQuery('page');
        $Cuser = $this->Cuser;
        $wx = Wx::findFirstByuid($Cuser->uid);
        $token = $wx->token;
        $coupon = Coupon::find("token='" . $token . "' AND del=0");
//        $this->view->setVar("coupon", $coupon);
        $paginator = new \Phalcon\Paginator\Adapter\Model(array(
            "data" => $coupon,
            "limit" =>5,
            "page" => $currentPage,
        ));
        $this->view->page = $paginator->getPaginate();
    }

    public function createAction()
    {
        $Cuser = $this->Cuser;
        $wx = Wx::findFirstByuid($Cuser->uid);
        $token = $wx->token;
        if ($this->request->isPost() == true) {
            $coupon = new Coupon();
            $coupon->token = $token;
            $coupon->del=0;
            if ($coupon->save($this->request->getPost())) {
                $this->flash->warning("保存成功");
            } else {
                $this->flash->warning("保存失败");
            }
        }
    }

    public function updateAction($id)
    {
        $Cuser = $this->Cuser;
        $wx = Wx::findFirstByuid($Cuser->uid);
        $token = $wx->token;
        $coupon = Coupon::findFirst("token='" . $token . "' AND id=" . $id);
        $this->view->setVar("coupon", $coupon);
        if ($this->request->isPost() == true) {
            if ($coupon->save($this->request->getPost())) {
                $this->flash->warning("更新成功");
            } else {
                $this->flash->warning("更新失败");
            }
        }
    }

    public function deleteAction($id)
    {
        $this->view->disable();
        $Cuser = $this->Cuser;
        $wx = Wx::findFirstByuid($Cuser->uid);
        $token = $wx->token;
        $coupon = Coupon::findFirst("token='" . $token . "' AND id=" . $id);
        if ($coupon) {
            $coupon->delete();
            $status = 'success';
        } else {
            $status = 'error';
        }
        return json_encode(array('status' => $status));
    }

    public function dashboardAction()
    {
    }

    public function cardAction($id)
    {

        $Cuser = $this->Cuser;
        $wx = Wx::findFirstByuid($Cuser->uid);
        $token = $wx->token;
        $card = CouponCard::find("pid=" . $id . " AND token='" . $token . "' AND del=0");
        $this->view->setVar("card", $card);
        if ($this->request->isPost() == true) {
            if($this->request->getPost("pic")==""||$this->request->getPost("name")=="") return $this->flash->warning("请输入活动名和图片");
            $card = new CouponCard();
            $card->token = $token;
            $card->pid = $id;
            $card->del = 0;
            if ($card->save($this->request->getPost())) {
                return $this->response->redirect("admin/appcoupon/card/".$id);
            } else {
                $this->flash->warning("保存失败");
            }
        }
    }

    public function delcardAction($id)
    {
        $this->view->disable();
        $Cuser = $this->Cuser;
        $wx = Wx::findFirstByuid($Cuser->uid);
        $token = $wx->token;
        $card = CouponCard::findFirst("token='" . $token . "' AND id=" . $id);
        if ($card) {
            $card->delete();
            $status = 'success';
        } else {
            $status = 'error';
        }
        return json_encode(array('status' => $status));
    }

    public function destoryAction($id)
    {
        $now = date("Y-m-d H:i:s", strtotime("now"));
        if ($this->request->isPost() == true) {
            $unique = $this->request->getPost('unique');
            $usercard = CouponUser::findFirst("unique='" . $unique . "' AND pid=".$id);
//            print_r($usercard->toArray());
            if ($usercard) {
                if ($usercard->del == 1) return $this->flash->warning("该优惠券已经被使用");
                if ($usercard->getCouponCard()->start <= $now and $now <= $usercard->getCouponCard()->end) {
                    $usercard->del = 1;
                    if ($usercard->update()) {
                        $this->flash->warning("该优惠券被成功使用");
                    }
                } else {
                    $this->flash->warning("优惠券不在有效期");
                }
            } else {
                $this->flash->warning("无效优惠券");
            }
        }
    }
}